--赛程
local GuildWarScheduleTabPanel = app.mvc.createPanelClass(...)

local ListViewVertical = require("app.widget.listview.ListViewVertical")
local GuildWarScheduleTitleCell = import("..renders.GuildWarScheduleTitleCell")
local GuildWarScheduleInfoCell = import("..renders.GuildWarScheduleInfoCell")
--data =
function GuildWarScheduleTabPanel.createWithData(data, node)
    local sprite = GuildWarScheduleTabPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function GuildWarScheduleTabPanel.create(node)
    return GuildWarScheduleTabPanel.createWithData(nil, node)
end

function GuildWarScheduleTabPanel:init(data)
    self.super.init(self, data)
  
    local params = {
        cellConfigs = {
            [1] = {renderClass = GuildWarScheduleTitleCell, perNum = 1},
            [2] = {renderClass = GuildWarScheduleInfoCell, perNum = 1},
        },
        cellTagMarkFunc = function(data)
            return data.tag
        end,
        cellEventProxy = function(eventName, params)
			
		end,
    }   
    self._listview = ListViewVertical.new(self, params)
    self:registerViewEvent({
        {name = ViewEvent.CSGuildWar_ScheduleInfo, callback = handler(self, self._onGetScheduleInfo)},
       
        
    })
end


function GuildWarScheduleTabPanel:onTabSelect()
    UD.csGuildWarData:C2S_WorldGuildWarMatch()

end


function GuildWarScheduleTabPanel:_onGetScheduleInfo(eventName, datas)
    local listViewData = {}
    
    local round = app:getWeekDayPassDailyUpdate()
    if round < 6 then
        -- 预选赛阶段
        table.sort(datas, function(a, b)
            if a:getRound() == b:getRound() then
                local aguild1 = a:getGuildWithSort()
                local bguild1 = b:getGuildWithSort()
                if aguild1 and bguild1 then
                    return aguild1:getRank() < bguild1:getRank()
                end   
            end
            return a:getRound() > b:getRound()
        end)
    else
        -- 决赛阶段
        table.sort(datas, function(a, b)
            if a:getRound() == b:getRound() then
                local aguild1 = a:getGuildWithSort()
                local bguild1 = b:getGuildWithSort()
                if aguild1 and bguild1 then
                    return aguild1:getRank() < bguild1:getRank()
                end   
            end
            return a:getRound() < b:getRound()
        end)
    end

    local curRound = -1    
    for k, v in pairs(datas) do
        if v:getRound() ~= curRound then
            curRound = v:getRound()
            table.insert(listViewData, {tag = 1, round = v:getRound()})
        end
        table.insert(listViewData, {tag = 2, matchInfo = v})
    end
    self._listview:reloadData(listViewData)
    if #listViewData == 0 then
        EM:notify(ViewEvent.CSGuildWar_PanelNotData, self._tabPanelIndex)
    end
end

function GuildWarScheduleTabPanel:setPanelTabIndex(index)
    self._tabPanelIndex = index
end


-- function GuildWarScheduleTabPanel:getTestData()
--     local data = {}
--     for i = 1, 10 do
--         if i % 4 == 0 then
--             table.insert( data, {tag = 1})
--         else
--             table.insert( data, {tag = 2})
--         end
        
--     end
--     return data
-- end


return GuildWarScheduleTabPanel